package com.huqing.icu.utils;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;


public class IpUtil {

    private static final String IP_REG = "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";
    private static final String LOCALHOST_ADDRESS = "0:0:0:0:0:0:0:1";
    private static final String LOCALHOST_IP = "127.0.0.1";
    private static final String SEPERATOR = ",";

    public IpUtil() {
    }

    public static boolean isValidIP(String ip) {
        if (StringUtils.isBlank(ip)) {
            return false;
        } else {
            Pattern patt = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$");
            Matcher mat = patt.matcher(ip);
            return mat.matches();
        }
    }

    public static String filterDuplicatedIP(String ipList) {
        String[] ipArray = ipList.split("\\,");
        Set<String> ipSet = new LinkedHashSet();

        for (int i = 0; i < ipArray.length; ++i) {
            ipSet.add(ipArray[i]);
        }

        return StringUtils.join(ipSet, ",");
    }

    public static boolean isInIpArray(String ip, String[] ipArray) {
        for (int i = 0; i < ipArray.length; ++i) {
            if (ip.equalsIgnoreCase(ipArray[i].trim())) {
                return true;
            }
        }

        return false;
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ipArray = request.getHeader("x-forwarded-for");
        String ip = getFirstIp(ipArray);
        if (isInvalidIP(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (isInvalidIP(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (isInvalidIP(ip)) {
            ip = request.getRemoteAddr();
        }

        ip = convertLocalhostFromV6toV4(ip);
        if (!ip.matches("^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$")) {
            //throw new ParameterException("Ip格式不合法: " + ip);
            return ip;
        } else {
            return ip;
        }
    }

    private static String convertLocalhostFromV6toV4(String ip) {
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    private static boolean isInvalidIP(String ip) {
        return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
    }

    private static String getFirstIp(String ip) {
        if (StringUtils.isNotBlank(ip)) {
            String[] ipArray = ip.split(",");
            if (ipArray.length > 1) {
                ip = ipArray[0].trim();
            }
        }

        return ip;
    }
}
